package com.cicc.its.txn.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.cicc.its.txn.dto.CashFlowDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
public class TxnDataController {

    private static final int BATCH_SIZE = 100000; // 每批处理的数据量

    @GetMapping("/exec")
    public String exec() {
        try (ExcelWriter excelWriter = ExcelUtil.getBigWriter()) {
            excelWriter.setDestFile(new File("/app/2024-04-26-资金流水.xlsx"));
            excelWriter.setOnlyAlias(true);
            excelWriter.renameSheet(CharSequenceUtil.format("资金流水"));
            List<CashFlowDto> cashFlowDtoList = new ArrayList<>();
            for (int i = 0; i < 5000000; i++) {
                CashFlowDto cashFlowDto = CashFlowDto.builder().account("account" + i)
                        .market(String.valueOf(RandomUtil.randomInt(2)))
                        .bizAmount(RandomUtil.randomBigDecimal(new BigDecimal("100000"),
                                new BigDecimal("1000000000")))
                        .bizName("bizName" + i)
                        .commission(RandomUtil.randomBigDecimal(new BigDecimal("100000"),
                                new BigDecimal("1000000000")))
                        .fundBalance(RandomUtil.randomBigDecimal(new BigDecimal("100000"),
                                new BigDecimal("1000000000")))
                        .matchSN(IdUtil.fastSimpleUUID())
                        .matchAmount(RandomUtil.randomBigDecimal(new BigDecimal("100000"),
                                new BigDecimal("1000000000")))
                        .currency("CNY")
                        .orderId(IdUtil.fastSimpleUUID())
                        .custName("custName" + i)
                        .matchPrice(RandomUtil.randomBigDecimal(new BigDecimal("100000"),
                                new BigDecimal("1000000000")))
                        .matchQty(RandomUtil.randomLong(1000))
                        .secuAcc("secuAcc" + i)
                        .occurDate(DateUtil.format(DateUtil.date(), "yyyyMMdd"))
                        .build();
                cashFlowDtoList.add(cashFlowDto);

                // 如果达到BATCH_SIZE，则写入一次并清空列表
                if ((i + 1) % BATCH_SIZE == 0) {
                    excelWriter.write(cashFlowDtoList);
                    cashFlowDtoList.clear(); // 清空列表以释放内存
                    log.info("Written {} rows", i + 1);
                }
            }

            // 写入剩余的数据
            if (!cashFlowDtoList.isEmpty()) {
                excelWriter.write(cashFlowDtoList);
            }
        } catch (Exception e) {
            log.error("生成excel失败", e);
        }
        return "success";
    }
}
